class EditWordStorage

  def initialize(original_word, translations, invalid_translations)
    @original_word = original_word.to_s.strip
    @translations = translations.to_s.strip
    @invalid_translations = invalid_translations.to_s.strip
  end

  #method should return new storage
  def self.from_word(word)
    new(word.original_word, trans_to_string(word.translations, true), trans_to_string(word.translations, false))
  end

  def try_to_save(user_id)
    raise CreateWordHelper::CreationErrors,
          (I18n.t :word_is_empty) if @original_word == nil || @original_word == CreateWordHelper::EMPTY
    raise CreateWordHelper::CreationErrors,
          (I18n.t :word_currently_exist) if is_word_exists?(@original_word)

    word = Word.new(
        :user_id => user_id,
        :original_word => @original_word,
        :translations => get_trans_array)
    word.save
  end

  def try_to_edit(word_id, user_id)
    raise CreateWordHelper::CreationErrors,
          (I18n.t :word_is_empty) if @original_word == nil || @original_word == CreateWordHelper::EMPTY
    raise CreateWordHelper::CreationErrors,
          (I18n.t :word_currently_exist) if !can_be_edited?(@original_word, word_id, user_id)

    word = Word.find_by_id(word_id)
    word.original_word = @original_word
    word.translations = get_trans_array
    word.save
  end

  def original_word
    @original_word
  end

  def translations
    @translations
  end

  def invalid_translations
    @invalid_translations
  end

  private
  def is_word_exists?(word_name)
    word = Word.find_by_original_word(word_name)
    word != nil
  end

  private
  def can_be_edited?(word_name, id, user_id)
    word = Word.find_by_original_word(word_name)

    if word == nil
      return true
    end

    if word != nil && word.user_id == user_id && word.id == Integer(id)
      true
    else
      false
    end
  end

  def create_translations(trans)
    raise CreateWordHelper::CreationErrors,
          (I18n.t :translations_is_empty) if trans == nil || trans == CreateWordHelper::EMPTY

    splitted = trans.split(CreateWordHelper::SPLITTER)

    ret_val = Array.new

    splitted.each do |tr|
      trimmed = tr.strip
      if !ret_val.include? trimmed
        if trimmed != CreateWordHelper::EMPTY
          ret_val << trimmed
        end
      end
    end

    raise CreateWordHelper::CreationErrors,
          (I18n.t :translations_is_empty) if ret_val.empty?
    ret_val
  end

  def self.trans_to_string(trans, is_valid)
    ret_str = CreateWordHelper::EMPTY.clone
    trans.each do |t|
      if t.is_valid == is_valid
        ret_str << t.trans
        ret_str << CreateWordHelper::SPACE.clone
        ret_str << CreateWordHelper::SPLITTER.clone
      end
    end

    if ret_str.length > 2
      ret_str = ret_str[0...(ret_str.length - 2)]
    end

    ret_str
  end

  #TODO
  def get_trans_array
    trans_arr = Array.new
    str_trans = create_translations(@translations)
    str_inv_trans = create_translations(@invalid_translations)

    str_trans.each do |s|
      trans_arr << Translation.new(:trans => s, :is_valid => 't')
    end

    str_inv_trans.each do |s|
      trans_arr << Translation.new(:trans => s, :is_valid => 'f')
    end

    trans_arr
  end
end